1 package edu.jiangxin.apktoolbox.file.zhconvert;
2
3 import com.github.houbb.opencc4j.util.ZhConverterUtil;
4 import edu.jiangxin.apktoolbox.swing.extend.FileListPanel;
5 import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
6 import edu.jiangxin.apktoolbox.utils.Constants;
7 import edu.jiangxin.apktoolbox.utils.FileUtils;
8 import org.apache.commons.lang3.StringUtils;
9
10 import javax.swing.*;
11 import java.awt.*;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.Serial;
17 import java.util.List;
18 import java.util.*;
19
20 public class ZhConvertPanel extends EasyPanel {
21 @Serial
22 private static final long serialVersionUID = 1L;
23
24 private JPanel northPanel;
25
26 private JSplitPane centerPanel;
27
28 private FileListPanel fileListPanel;
29
30 private JTextField suffixTextField;
31
32 private JCheckBox recursiveCheckBox;
33
34 private JComboBox<String> comboBox;
35
36 private JTextField keyText;
37
38 private JTextField valueText;
39
40 private JTextArea textArea;
41
42 private JList<Object> transformList;
43
44 private static ZHConverterUtils myZHConverterUtils = new ZHConverterUtils();
45
46 public ZhConvertPanel() throws HeadlessException {
47 super();
48 }
49
50 @Override
51 public void initUI() {
52
53 BorderLayout boxLayout = new BorderLayout();
54 setLayout(boxLayout);
55
56 createNorthPanel();
57 add(northPanel, BorderLayout.NORTH);
58
59 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
60
61 createCenterPanel();
62 add(centerPanel, BorderLayout.CENTER);
63 }
64
65 private void createNorthPanel() {
66 northPanel = new JPanel();
67 northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.Y_AXIS));
68
69 fileListPanel = new FileListPanel();
70 fileListPanel.initialize();
71 northPanel.add(fileListPanel);
72 northPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
73
74 JPanel optionPanel = new JPanel();
75 optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.X_AXIS));
76 northPanel.add(optionPanel);
77
78 JLabel suffixLabel = new JLabel("Suffix:");
79 suffixTextField = new JTextField();
80 suffixTextField.setToolTipText("an array of extensions, ex. {\"java\",\"xml\"}. If this parameter is empty, all files are returned.");
81 suffixTextField.setText(conf.getString("osconvert.suffix"));
82 optionPanel.add(suffixLabel);
83 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
84 optionPanel.add(suffixTextField);
85 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
86
87 recursiveCheckBox = new JCheckBox("Recursive");
88 recursiveCheckBox.setSelected(true);
89 optionPanel.add(recursiveCheckBox);
90 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
91
92 comboBox = new JComboBox<>();
93 comboBox.addItem(Constants.zhSimple2zhTw);
94 comboBox.addItem(Constants.zhTw2zhSimple);
95 optionPanel.add(comboBox);
96
97 JButton convertBtn = new JButton("确认转换");
98 convertBtn.addActionListener(new ConvertBtnActionListener());
99 northPanel.add(convertBtn);
100
101 JFileChooser fileChooser = new JFileChooser();
102 fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
103 }
104
105 private void createCenterPanel() {
106 JPanel centerLeftTopPanel = new JPanel();
107 centerLeftTopPanel.setLayout(new BoxLayout(centerLeftTopPanel, BoxLayout.Y_AXIS));
108
109 JPanel keyValuePanel = new JPanel();
110 keyValuePanel.setLayout(new BoxLayout(keyValuePanel, BoxLayout.X_AXIS));
111 centerLeftTopPanel.add(keyValuePanel);
112
113 keyText = new JTextField(10);
114 keyValuePanel.add(keyText);
115
116 valueText = new JTextField(10);
117 keyValuePanel.add(valueText);
118
119 JButton saveBtn = new JButton("添加词组定义");
120 saveBtn.addActionListener(new SaveBtnActionListener());
121 centerLeftTopPanel.add(saveBtn);
122
123 JScrollPane centerLeftBottomPanel = new JScrollPane();
124 textArea = new JTextArea();
125 textArea.setMargin(new Insets(10, 10, 10, 10));
126
127 textArea.setLineWrap(true);
128 textArea.setEditable(false);
129 centerLeftBottomPanel.add(textArea);
130
131
132 centerLeftBottomPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
133 JSplitPane centerLeftSplitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, centerLeftTopPanel, centerLeftBottomPanel);
134
135 transformList = new JList<>();
136 refreshListData();
137 transformList.setFont(new Font("Dialog", 1, 18));
138 transformList.setBorder(BorderFactory.createTitledBorder("词组转换定义"));
139 JScrollPane centerRightScrollPanel = new JScrollPane(transformList);
140
141 centerPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, centerLeftSplitPanel, centerRightScrollPanel);
142 centerPanel.setDividerLocation(0.7f);
143 }
144
145 private final class ConvertBtnActionListener implements ActionListener {
146 @Override
147 public void actionPerformed(ActionEvent e) {
148 new Thread(() -> {
149 String converType = comboBox.getSelectedItem().toString();
150 System.out.println(converType);
151
152 List<File> fileList = new ArrayList<>();
153 for (File file : fileListPanel.getFileList()) {
154 String[] extensions = null;
155 if (StringUtils.isNotEmpty(suffixTextField.getText())) {
156 extensions = suffixTextField.getText().split(",");
157 }
158 fileList.addAll(FileUtils.listFiles(file, extensions, recursiveCheckBox.isSelected()));
159 }
160 Set<File> fileSet = new TreeSet<>(fileList);
161 fileList.clear();
162 fileList.addAll(fileSet);
163
164 textArea.setCaretPosition(textArea.getText().length());
165 try {
166 scanFolderAndConver(fileList, converType, textArea);
167 JOptionPane.showMessageDialog(getFrame(), "转换成功" , "提示",JOptionPane.WARNING_MESSAGE);
168 textArea.append("done..." + "\n");
169 textArea.setCaretPosition(textArea.getText().length());
170 } catch (IOException e1) {
171 JOptionPane.showMessageDialog(getFrame(), "异常:" + e1.getMessage(), "异常",JOptionPane.ERROR_MESSAGE);
172 textArea.append("转换异常..." + "\n");
173 textArea.setCaretPosition(textArea.getText().length());
174 }
175 }).start();
176 }
177 }
178
179 private final class SaveBtnActionListener implements ActionListener {
180 @Override
181 public void actionPerformed(ActionEvent e) {
182 String key = keyText.getText();
183 String value = valueText.getText();
184
185 if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)){
186 myZHConverterUtils.storeDataToProperties(key,value);
187 JOptionPane.showMessageDialog(ZhConvertPanel.this, "成功插入一条词组对应信息", "提示",JOptionPane.WARNING_MESSAGE);
188 refreshListData();
189 textArea.append("成功插入一条词组对应信息:" + key + " <===> " + value + "\n");
190 }else{
191 JOptionPane.showMessageDialog(ZhConvertPanel.this, "键值对不能为空", "提示",JOptionPane.WARNING_MESSAGE);
192 }
193 }
194 }
195
196 private void refreshListData(){
197 List<String> listModel = new ArrayList<>();
198 Properties properties = myZHConverterUtils.getCharMap();
199 for (String key2 : properties.stringPropertyNames()) {
200 listModel.add(key2 + " <===> " + properties.getProperty(key2));
201 }
202 transformList.setListData(listModel.toArray());
203 }
204
205 private static void scanFolderAndConver(List<File> fileList, String converType, JTextArea jTextArea) throws IOException {
206 jTextArea.append("文件转换开始:\n");
207 for (File file : fileList){
208 jTextArea.append("开始转换:"+file + "\n");
209 String content = org.apache.commons.io.FileUtils.readFileToString(file, "utf-8");
210
211 if (converType.equals(Constants.zhSimple2zhTw)){
212 String str = myZHConverterUtils.myConvertToTW(content);
213 String result = ZhConverterUtil.toTraditional(str);
214 org.apache.commons.io.FileUtils.write(file,result,"UTF-8");
215 }else{
216 String str = myZHConverterUtils.myConvertToSimple(content);
217 String result = ZhConverterUtil.toSimple(str);
218 org.apache.commons.io.FileUtils.write(file,result,"UTF-8");
219 }
220 jTextArea.append("转换完成:"+file + "\n");
221 jTextArea.setCaretPosition(jTextArea.getText().length());
222 }
223 jTextArea.append("文件转换结束:\n");
224 jTextArea.append("==========================================================================:\n");
225 }
226 }